var request = require('../../utils/request.js')
var util = require('../../utils/util.js')
import * as echarts from '../../component/ec-canvas/echarts';
var date = [], passed = [], rejected = [], total = [], max, min
Page({
    data: {
        newUser: 0,
        total: 0,
        pass: 0,
        refuse: 0,
        unexam: 0,
        echart: false,
        ec: {//折线图
            onInit: initChart
        }
    },
    onShow() {
        this.setData({
            echart: false
        })
        date = [], passed = [], rejected = [], total = []
        this.getInfo()
    },

    // 前往通知页面
    goToNotice(){
        qq.navigateTo({
            url: '../notice/notice'
        })
    },

    bindEndDateChange: function (e) {
        this.setData({
            endDate: e.detail.value
        })
    },
    bindStartDateChange: function (e) {
        this.setData({
            startDate: e.detail.value
        })
    },

    getInfo() {
        let that = this
        request.GET('/api/list/statistics').then(
            function (res) {
                // list是近14天的访客数据 list[0]是今天
                let list = res.data.data
                // 设置前两条
                that.setData({
                    newUser: list[0].visitors,
                    pass: list[0].passed,
                    refuse: list[0].rejected,
                    unexam: list[0].undefined,
                    total: list[0].passed + list[0].rejected + list[0].undefined
                })

                // 获取7天数据 设置折线图
                list = list.slice(0, 7)
                // 处理日期
                list = list.map((item) => {
                    let date = item.date.substr(5)
                    item.date = date
                    return item
                })

                // 不够7条补齐
                for (var i = list.length; i < 7; i++) {
                    list.push({
                        date: 0,
                        passed: 0,
                        rejected: 0,
                        undefined: 0,
                        visitors: 0
                    })
                }
                console.log(list)
                for (var j in list) {
                    date.unshift(list[j].date)
                    passed.unshift(list[j].passed)
                    rejected.unshift(list[j].rejected)
                    total.unshift(list[j].sum)
                    if (j == list.length - 1) {
                        var bigArr = total.concat(passed).concat(rejected)
                        max = arrayMax(bigArr)
                        min = arrayMin(bigArr)
                        that.setData({
                            echart: true
                        })
                    }
                }
            }
        )
    },
    echartInit(e) {
        console.log(e)
    },

    onShareAppMessage: function () {
        qq.showShareMenu({
            showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
        })
    },
})

//初始化折线图
function initChart(canvas, width, height, dpr) {
    const chart = echarts.init(
        canvas, //ec-canvas组件中的canvas
        null,
        {
            width: width,//绘图区域的宽和高，不包括标题和图例
            height: height,
            devicePixelRatio: dpr //分辨率
        }
    );

    canvas.setChart(chart);

    var option = {

        // //标题
        // title: {
        //     text: '数据分析',
        //     left: 'center'
        // },

        //图例
        legend: {
            data: ['总', '过', '拒'],
            // data:['A'],
            top: 30,
            left: 'center',
            backgroundColor: 'rgba(128, 128, 128, 0)'
        },

        //直角坐标系
        grid: {
            containLabel: true
        },
        // //所点击的横坐标对应的数据标签
        // tooltip: {
        //     show: true,
        //     trigger: 'axis'
        // },

        //横坐标
        xAxis: {
            type: 'category',//类目轴
            boundaryGap: false,//坐标轴两端空白策略
            data: date,//有多少显示多少（应用时考虑文字是否会挤在一起）
            show: true//是否显示
        },

        //纵坐标
        yAxis: {
            type: 'value',//数值轴
            splitLine: {//分割线
                lineStyle: {
                    type: 'dashed'//虚线
                }
            },
            show: true,
            max: max + 1,//最大数
            min: min,//最小数
            minInterval: 1,//最小间隔数
            maxInterval: 20,//最大间隔数
        },

        series: [{
            name: '总',//名字
            type: 'line',//线图
            smooth: false,//平滑
            data: total//数据
        }
            , {
            name: '过',
            type: 'line',
            smooth: false,
            data: passed
        }, {
            name: '拒',
            type: 'line',
            smooth: false,
            data: rejected
        }
        ]
    };
    console.log(option)
    chart.setOption(option);
    return chart;
}

function arrayMin(arr) {
    var len = arr.length, min = Infinity;
    while (len--) {
        if (arr[len] < min) {
            min = arr[len];
        }
    }
    return min;
};

function arrayMax(arr) {
    var len = arr.length, max = -Infinity;
    while (len--) {
        if (arr[len] > max) {
            max = arr[len];
        }
    }
    return max;
};